package com.tanhua.admin.controller;

/**
 * @Author: cl
 * @Date: 2022/5/13 - 05 - 13
 * @Description:
 */

import com.tanhua.admin.service.ManagerService;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 用户管理模块
 */
@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManagerService managerService;

    /**
     * 获取所有注册用户
     *
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/users")
    public ResponseEntity getUsers(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {

        PageResult pr = managerService.getUsers(page, pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 通过用户的id查询用户的详情
     *
     * @param userId
     * @return
     */
    @GetMapping("/users/{id}")
    public ResponseEntity getByUserId(@PathVariable("id") Long userId) {

        UserInfo userInfo = managerService.getByUserId(userId);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 根据用户的id查询用户所有小视频
     *
     * @param page
     * @param pagesize
     * @param uid
     * @return
     */
    @GetMapping("/videos")
    public ResponseEntity getVideosByUid(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize, Long uid) {

        PageResult pr = managerService.getVideosByUid(page, pagesize, uid);
        return ResponseEntity.ok(pr);
    }

    /**
     * 根据用户的id或审核状态分页查询用户所有动态
     *
     * @param page
     * @param pagesize
     * @param uid
     * @param state
     * @return
     */
    @GetMapping("/messages")
    public ResponseEntity getVideosByUid(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            Long uid, String state) {

        //处理state为''
        if(state==null||state.equals("")||state.equals("''")){
            state = null;
        }

        PageResult pr = managerService.getMovementByUidAndState(page, pagesize, uid, state);
        return ResponseEntity.ok(pr);
    }

    /**
     * 冻结用户
     *
     * @param params
     * @return
     */
    @PostMapping("/users/freeze")
    public ResponseEntity freeze(@RequestBody Map params) {

        Map map = managerService.freeze(params);
        return ResponseEntity.ok(map);
    }

    /**
     * 解冻用户
     *
     * @param params
     * @return
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity unfreeze(@RequestBody Map params) {
        Map map = managerService.userUnfreeze(params);
        return ResponseEntity.ok(map);
    }
}
